package com.guigu.controller.manufacture;


import com.github.pagehelper.PageInfo;
import com.guigu.pojo.design.DFile;
import com.guigu.pojo.manufacture.*;
import com.guigu.pojo.stock.SPay;
import com.guigu.pojo.stock.SPayDetails;
import com.guigu.service.design.DFileService;
import com.guigu.service.manufacture.*;
import com.guigu.service.stock.SPayDetailsService;
import com.guigu.service.stock.SPayService;
import com.guigu.utils.CodeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mr.Zhang
 * @since 2024-12-06
 */
@Controller
@RequestMapping("/m-manufacture")
public class MManufactureController {

    @Autowired
    private DFileService dFileService;

    @Autowired
    private MApplyService mApplyService;

    @Autowired
    private MManufactureService manufactureService;

    @Autowired
    private MProcedureService procedureService;

    @Autowired
    private MProcedureModuleService procedureModuleService;

    @Autowired
    private MDesignProcedureService designProcedureService;

    @Autowired
    private MDesignProcedureDetailsService designProcedureDetailsService;

    @Autowired
    private MDesignProcedureModuleService designProcedureModuleService;

    @Autowired
    public SPayService sPayService;

    @Autowired
    public SPayDetailsService sPayDetailsService;

    @GetMapping("/Formulate_production_dispacth_order")
    public String Formulate_production_dispacth_order(){
        return "jsp/manufacture/e/Formulate_production_dispacth_order";
    }

    @GetMapping("/Inquiry_production_dispatch")
    public String Inquiry_production_dispatch(){
        return "jsp/manufacture/e/Inquiry_production_dispatch";
    }

    @GetMapping("/Inquiry_producttion_dispatch_query")
    public String Inquiry_producttion_dispatch_query(){
        return "jsp/manufacture/e/Inquiry_producttion_dispatch_query";
    }

    @GetMapping("/Production_Dispatch_Order")
    public String Production_Dispatch_Order(){
        return "jsp/manufacture/e/Production_Dispatch_Order";
    }

    @GetMapping("/production_Dispatch_order_preview")
    public String production_Dispatch_order_preview(){
        return "jsp/manufacture/e/production_Dispatch_order_preview";
    }

    @GetMapping("/production_dispatch_order_review")
    public String production_dispatch_order_review(){
        return "jsp/manufacture/e/production_dispatch_order_review";
    }

    @GetMapping("/production_dispatch_order_review_sub")
    public String production_dispatch_order_review_sub(){
        return "jsp/manufacture/e/production_dispatch_order_review_sub";
    }

    @PostMapping("/createADispatchTicket")
    @ResponseBody
    public Boolean createADispatchTicket(@RequestBody HashMap<String, Object> map){
        System.out.println(map);
        Integer id = null;
        String designer = "";
        String register = "";
        String remark = "";

        if(map.containsKey("id")){
            id = Integer.parseInt(map.get("id").toString());
        }
        if(map.containsKey("designer")){
            designer = map.get("designer").toString();
        }
        if(map.containsKey("register")){
            register = map.get("register").toString();
        }
        if(map.containsKey("remark")){
            remark = map.get("remark").toString();
        }

        MApply mApply = mApplyService.getById(id);
        DFile dFile = dFileService.selectByProductId(mApply.getProductId());
        MDesignProcedure mDesignProcedure = designProcedureService.selectById(id);
        ArrayList<MDesignProcedureDetails> mDesignProcedureDetails = designProcedureDetailsService.selectArrByParentId(mDesignProcedure.getId());


        MManufacture endManufacture = manufactureService.getEndManufacture();
        CodeUtils codeUtils = new CodeUtils();
        String manufactureId = codeUtils.getManufactureId(endManufacture);

        MManufacture manufacture = new MManufacture();
        manufacture.setManufactureId(manufactureId);
        manufacture.setProductId(mApply.getProductId());
        manufacture.setProductName(mApply.getProductName());
        manufacture.setAmount(mApply.getAmount());
        manufacture.setApplyIdGroup(mApply.getApplyId());
        manufacture.setProductDescribe(mApply.getProductDescribe());
        manufacture.setModuleCostPriceSum(mDesignProcedure.getModuleCostPriceSum().multiply(mApply.getAmount()));
        manufacture.setLabourCostPriceSum(mDesignProcedure.getCostPriceSum().multiply(mApply.getAmount()));
        manufacture.setDesigner(designer);
        manufacture.setRegister(register);
        manufacture.setRegisterTime(new Date());
        manufacture.setRemark(remark);
        manufacture.setCheckTag("0");
        manufacture.setManufactureProcedureTag("0");

        boolean save = manufactureService.save(manufacture);

        for (int i = 0; i < mDesignProcedureDetails.size(); i++) {
            MDesignProcedureDetails mDesignProcedureDetails1 = mDesignProcedureDetails.get(i);

            ArrayList<MDesignProcedureModule> mDesignProcedureModules = designProcedureModuleService.selectArrByParentId(mDesignProcedureDetails1.getId());

            MProcedure mProcedure = new MProcedure();
            mProcedure.setParentId(manufacture.getId());
            mProcedure.setDetailsNumber(mDesignProcedureDetails1.getDetailsNumber());
            mProcedure.setProcedureId(mDesignProcedureDetails1.getProcedureId());
            mProcedure.setProcedureName(mDesignProcedureDetails1.getProcedureName());
            mProcedure.setLabourHourAmount(mDesignProcedureDetails1.getLabourHourAmount() != null?mDesignProcedureDetails1.getLabourHourAmount().multiply(mApply.getAmount()):BigDecimal.valueOf(0));
            mProcedure.setSubtotal(mDesignProcedureDetails1.getSubtotal()!=null?mDesignProcedureDetails1.getSubtotal().multiply(mApply.getAmount()):BigDecimal.valueOf(0));
            mProcedure.setModuleSubtotal(mDesignProcedureDetails1.getModuleSubtotal()!=null?mDesignProcedureDetails1.getModuleSubtotal().multiply(mApply.getAmount()):BigDecimal.valueOf(0));
            mProcedure.setCostPrice(mDesignProcedureDetails1.getCostPrice());

            ArrayList<MProcedureModule> mProcedureModules = new ArrayList<>();
            BigDecimal Amount = BigDecimal.valueOf(0);
            for (int j = 0; j < mDesignProcedureModules.size(); j++) {
                MDesignProcedureModule mDesignProcedureModule = mDesignProcedureModules.get(j);
                MProcedureModule mProcedureModule = new MProcedureModule();
                mProcedureModule.setDetailsNumber(mDesignProcedureModule.getDetailsNumber());
                mProcedureModule.setProductId(mDesignProcedureModule.getProductId());
                mProcedureModule.setProductName(mDesignProcedureModule.getProductName());
                mProcedureModule.setCostPrice(mDesignProcedureModule.getCostPrice());
                BigDecimal amount = mDesignProcedureModule.getAmount().multiply(mApply.getAmount());
                Amount = amount.add(Amount);
                mProcedureModule.setAmount(amount);
                mProcedureModule.setRealAmount(BigDecimal.valueOf(0));
                mProcedureModule.setRenewAmount(BigDecimal.valueOf(0));
                mProcedureModule.setSubtotal(amount.multiply(mDesignProcedureModule.getCostPrice()));
                mProcedureModule.setRealSubtotal(BigDecimal.valueOf(0));
                mProcedureModules.add(mProcedureModule);
            }
            mProcedure.setDemandAmount(Amount);
            mProcedure.setRealAmount(BigDecimal.valueOf(0));
            mProcedure.setProcedureFinishTag("0");
            mProcedure.setProcedureTransferTag("0");

            boolean save1 = procedureService.save(mProcedure);
            for (int k = 0; k < mProcedureModules.size(); k++) {
                MProcedureModule mProcedureModule = mProcedureModules.get(k);
                mProcedureModule.setParentId(mProcedure.getId());
                boolean save2 = procedureModuleService.save(mProcedureModule);
            }
        }
        mApply.setManufactureTag("1");
        boolean b = mApplyService.updateById(mApply);
        return b;
    }

    @PostMapping("/initial")
    @ResponseBody
    public PageInfo<MManufacture> initial(@RequestBody HashMap<String, Object> map) {
        PageInfo<MManufacture> mManufacturePageInfo = manufactureService.selectArrByPage(map);
        return mManufacturePageInfo;
    }

    @GetMapping("/selectById")
    @ResponseBody
    public MManufacture selectById(HttpServletRequest req){
        Integer id = StringUtils.isNotBlank(req.getParameter("id")) ? Integer.parseInt(req.getParameter("id")) : null;
        MManufacture mManufacture = manufactureService.getById(id);
        return mManufacture;
    }

    @PostMapping("/audit")
    @ResponseBody
    public Boolean audit(@RequestBody HashMap<String, Object> map) {
        Integer id = null;
        String checkTag = "";
        String checker = "";
        if (map.containsKey("id")) {
            id = Integer.parseInt(map.get("id").toString());
        }
        if (map.containsKey("checkTag")) {
            checkTag = map.get("checkTag").toString();
        }
        if (map.containsKey("checker")) {
            checker = map.get("checker").toString();
        }
        if (checkTag == ""||checkTag ==null){
            return false;
        }
        MManufacture manufacture = manufactureService.getById(id);
        if(checkTag == "2"){
            manufacture.setCheckTag(checkTag);
            boolean b = manufactureService.updateById(manufacture);
            return b;
        }
        ArrayList<MProcedure> mProcedures = procedureService.selectArrByParentId(id);
        for (int i = 0; i < mProcedures.size(); i++) {
            MProcedure mProcedure = mProcedures.get(i);

            if(mProcedure.getDemandAmount() == null || BigDecimal.valueOf(0).equals(mProcedure.getDemandAmount())){
                continue;
            }
            ArrayList<MProcedureModule> mProcedureModules = procedureModuleService.initialModule(mProcedure.getId());

            SPay endSpay = sPayService.getEndSpay();
            CodeUtils codeUtils = new CodeUtils();
            String outboundCode = codeUtils.getOutboundCode(endSpay);

            SPay sPay = new SPay();
            sPay.setPayId(outboundCode);
            sPay.setReasonexact(manufacture.getManufactureId()+"-"+mProcedure.getProcedureId());
            sPay.setAmountSum(mProcedure.getDemandAmount());
            sPay.setCostPriceSum(manufacture.getModuleCostPriceSum());
            sPay.setReason("内部领料");
            sPay.setCheckTag("1");
            sPay.setChecker(checker);
            sPay.setCheckTime(new Date());
            sPay.setStoreTag("1");
            sPay.setPaidAmountSum(BigDecimal.valueOf(0));
            sPay.setPayCheckTag("0");

            boolean save = sPayService.save(sPay);

            for (int j = 0; j < mProcedureModules.size(); j++) {
                MProcedureModule mProcedureModule = mProcedureModules.get(j);

                DFile dFile = dFileService.selectByProductId(mProcedureModule.getProductId());

                SPayDetails sPayDetails = new SPayDetails();
                sPayDetails.setParentId(sPay.getId());
                sPayDetails.setProductId(mProcedureModule.getProductId());
                sPayDetails.setProductName(mProcedureModule.getProductName());
                sPayDetails.setProductDescribe(dFile.getProductDescribe());
                sPayDetails.setAmount(mProcedureModule.getAmount());
                sPayDetails.setAmountUnit(dFile.getAmountUnit());
                sPayDetails.setCostPrice(mProcedureModule.getCostPrice());
                sPayDetails.setSubtotal(mProcedureModule.getAmount().multiply(mProcedureModule.getCostPrice()));
                sPayDetails.setPayTag("0");
                sPayDetails.setPaidAmount(BigDecimal.valueOf(0));

                boolean save1 = sPayDetailsService.save(sPayDetails);
            }
        }
        manufacture.setCheckTag(checkTag);
        boolean b = manufactureService.updateById(manufacture);
        return b;
    }

}

